#include "stdafx.h"
#include "wf_modifytrim.h"

#include "pf_snapper.h"


/**
 * @param both Trim both entities.
 */
WF_ModifyTrim::WF_ModifyTrim(PF_Container& container,
        PF_GraphicView& graphicView, bool both)
        :PF_PreviewActionInterface("Trim Entity",
                           container, graphicView) {

    trimEntity = NULL;
    trimCoord = PF_Vector(false);
    limitEntity = NULL;
    limitCoord = PF_Vector(false);
    this->both = both;
}

QAction* WF_ModifyTrim::createGUIAction(LX_Define::ActionType type, QObject* /*parent*/) {
    QAction* action;
	
	switch (type) {
	default:
	case LX_Define::ActionModifyTrim:
		// (tr("Trim")
		action = new QAction(tr("&Trim"), NULL);
		action->setIcon(QIcon(":/extui/modifytrim.png"));
	    //action->zetStatusTip(tr("Trim Entities"));
		break;

	case LX_Define::ActionModifyTrim2:
		// tr("Trim Two")
		action = new QAction(tr("&Trim Two"), NULL);
		action->setIcon(QIcon(":/extui/modifytrim2.png"));
        //action->zetStatusTip(tr("Trim two Entities"));
		break;
	}
	
    return action;
}


void WF_ModifyTrim::init(int status) {
    PF_ActionInterface::init(status);

    snapMode = LX_Define::SnapFree;
    snapRes = LX_Define::RestrictNothing;

}



void WF_ModifyTrim::trigger() {

    PF_DEBUG->print("PF_ActionModifyTrim::trigger()");

    if (trimEntity!=NULL && trimEntity->isAtomic() &&
            limitEntity!=NULL /* && limitEntity->isAtomic()*/) {

        PF_Modification m(*container, graphicView);
        m.trim(trimCoord, (PF_AtomicEntity*)trimEntity,
               limitCoord, /*(PF_AtomicEntity*)*/limitEntity,
               both);

        trimEntity = NULL;
        if (both) {
            limitEntity->setHighlighted(false);
            graphicView->drawEntity(limitEntity);
            setStatus(ChooseLimitEntity);
        } else {
            setStatus(ChooseTrimEntity);
        }

        PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
    }
}



void WF_ModifyTrim::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionModifyTrim::mouseMoveEvent begin");

    PF_Vector mouse = graphicView->toGraph(e->x(), e->y());
    PF_Entity* se = catchEntity(e);

    switch (getStatus()) {
    case ChooseLimitEntity:
        limitCoord = mouse;
        limitEntity = se;
        break;

    case ChooseTrimEntity:
        trimCoord = mouse;
        trimEntity = se;
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionModifyTrim::mouseMoveEvent end");
}



void WF_ModifyTrim::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {

        PF_Vector mouse = graphicView->toGraph(e->x(), e->y());
        PF_Entity* se = catchEntity(e);

        switch (getStatus()) {
        case ChooseLimitEntity:
            limitCoord = mouse;
            limitEntity = se;
            if (limitEntity!=NULL /*&& limitEntity->isAtomic()*/) {
                limitEntity->setHighlighted(true);
                graphicView->drawEntity(limitEntity);
                setStatus(ChooseTrimEntity);
            }
            break;

        case ChooseTrimEntity:
            trimCoord = mouse;
            trimEntity = se;
            if (trimEntity!=NULL && trimEntity->isAtomic()) {
                trigger();
            }
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        if (limitEntity!=NULL) {
            limitEntity->setHighlighted(false);
            graphicView->drawEntity(limitEntity);
        }
        init(getStatus()-1);
    }
}



void WF_ModifyTrim::updateMouseButtonHints() {
    switch (getStatus()) {
    case ChooseLimitEntity:
        if (both) {
            PF_DIALOGFACTORY->updateMouseWidget(tr("Select first trim entity"),
                                                tr("Cancel"));
        } else {
            PF_DIALOGFACTORY->updateMouseWidget(tr("Select limiting entity"),
                                                tr("Back"));
        }
        break;
    case ChooseTrimEntity:
        if (both) {
            PF_DIALOGFACTORY->updateMouseWidget(tr("Select second trim entity"),
                                                tr("Cancel"));
        } else {
            PF_DIALOGFACTORY->updateMouseWidget(tr("Select entity to trim"),
                                                tr("Back"));
        }
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ModifyTrim::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyTrim::updateToolBar() {
    PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
}


